<?php
/*
 * 优惠劵类
 *
 */
namespace Home\Logic;

class CouponLogic{
    
    const IS_DELETE = 2;
    
    public function __construct() {
        $this->c = D('Coupon');
        import('Memcachectr');
        $this->m = \Think\Memcachectr::create();
    }

    public function insertHui($data){
    	return $this->c->add($data);
    }

    public function getHuiById($id){
    	return $this->c->where(array('id'=>$id))->find();
    }

    public function updateHui($id,$data){
    	return $this->c->where(array('id'=>$id))->save($data);
    }

    public function lists($arr,$start=0,$length=12){
        if($arr['store_id']!=NULL && count($arr['store_id'])>0){
            $where[] = 'store_id in('.implode(',',(array)$arr['store_id']).')';
        }else{
            return ;
        }
        if($arr['type_name']){
            $where[] = 'type_name=\''.$arr['type_name'].'\'';
        }
        if(!isset($arr['status'])){
            $where[] = 'status!='.self::IS_DELETE;
        }else{
            $where[] = 'status='.$arr['status'];
        }
    	return $this->c->where(implode(' and ',$where))->order('id desc')->limit($start,$length)->select();  
    }


    public function ling($coupon_id){
        $l = D('Coupon_ling');
        $re = $l->where(array('coupon_id'=>$coupon_id))->order('id desc')->select();
        return $re;
    }

     public function update($id,$data){
        $l = D('Coupon_ling');
        return $l->where(array('id'=>$id))->save($data);
    }

    public function getCouponUser($coupon_id,$status=0,$search='',$start=0,$length=12){
        $re = $this->m->get('coupon_'.$coupon_id.'_'.$start);
        if(!$re){
            if(is_mobile($search)){
                $u = D('User','Logic');
                $user = $u->getUserByAccount($search);
                $search= $user['id'];
                $array['client_id']=$search;
            }elseif(strlen($search)>0){
                 $array['number']=$search;
            }
            if($status==2){
                $array['status']=0;
            }elseif($status==1){
                $array['status']=$status;
            }
            $array['coupon_id'] = $coupon_id;
            $l = D('Coupon_ling');
            $re =  $l->where($array)->order('id desc')->limit($start,$length)->select();
            $this->m->set('coupon_'.$coupon_id.'_'.$start,$re,true);
        }
            return $re;
        
    }
    public function getCouponUserCount($coupon_id,$status=0,$search=''){
        if(is_mobile($search)){
            $u = D('User','Logic');
            $user = $u->getUserByAccount($search);
            $search= $user['id'];
            $array['client_id']=$search;
        }elseif(strlen($search)>0){
             $array['number']=$search;
        }
        if($status==2){
            $array['status']=0;
        }elseif($status==1){
            $array['status']=$status;
        }
        $array['coupon_id'] = $coupon_id;
        $l = D('Coupon_ling');
        return $l->where($array)->count();
    }
    
    /*获取用户某个店铺的优惠卷，不区分店铺*/
    public function getUserCoupon($store_id,$user_id,$start,$length=12){
        $l = D('Coupon_ling');
        $where['store_id']=$store_id;
        $where['client_id']=$user_id;
        $where['status']=0;
        $coupons = $l->where($where)->order('id desc')->limit($start,$length)->select();
        return $coupons;
    }
    
    /*获取用户所有优惠卷，不区分店铺*/
    public function getUserCouponByUserId($user_id,$start,$length=12){
        $l = D('Coupon_ling');
        $where['client_id']=$user_id;
        $where['status']=0;
        $coupons = $l->where($where)->order('id desc')->limit($start,$length)->select();
        return $coupons;
    }
    
    public function getCouponsByStore($store_id,$start,$length=12){
        $time = time();
        $where = 'store_id='.$store_id.' and status=1 and end_date>'.$time.' and num>count and is_action=0';
        return $this->c->where($where)->order('id desc')->limit($start,$length)->select();
    }

    //优惠劵详情
    public function getCouponById($store_id,$coupon_id){
        $where = 'id='.$coupon_id.' and status=1 and store_id='.$store_id.' and num>count';
        return $this->c->where($where)->find();
    }

    //检查是否领取过
    public function double($data){
        $l = D('Coupon_ling');
        $where['client_id'] = $data['client_id'];
        $where['coupon_id'] = $data['coupon_id'];
        return $l->where($where)->find();
    }

    //领取前 count 领取
    public function count($coupon_id,$data){
        $this->c->startTrans();
        $re = $this->c->where(array('id'=>$coupon_id))->find();
        if($re['end_date']<time()){
            return false;
        }
        $count = $re['count'];
        if($count>=$re['num'] && $re['num']>0){
            return false;
        }
        $arr['count'] = $count+1;
        $counts = $this->c->where(array('id'=>$coupon_id))->save($arr);
        $l = D('Coupon_ling');
        $res = $l->add($data);
        if($counts==1 && $res>0){
            $this->c->commit();
        }else{
            $this->c->rollback();
        }
        return $res;
    }

    //与活动有关的优惠劵查询
    public function getCouponByAction($num,$store_id){
        $where = 'is_action=1 and status=1 and count=0 and num='.$num.' and store_id='.$store_id;
        return $this->c->where($where)->order('id desc')->find();
    }

    public function getHuiByActionId($action_id){
        $where= 'is_action='.$action_id;
        return $this->c->where($where)->order('id desc')->find();
    }
    //发送短信时用
    public function getCouponsByActionId($action_id){
        $where = 'is_action='.$action_id;
        return $this->c->where($where)->order('id asc')->select();
    }

}
    
